package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    //menu列表展示
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //查询条件
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //完成分页查询，基于mybatis框架提供方的分页助手插件完成
        PageHelper.startPage(currentPage,pageSize);

        Page<Menu> page =menuDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //menu添加
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }
    //menu删除
    @Override
    public void deleteById(Integer id) {
        //根据id查询是否有匹配的parentMenuId
        int number = menuDao.findParentMenuIdById(id);

        if (number>0){
            //当前检查项已经被关联到检查组，不允许删除
            new RuntimeException();
        }
        //根据id删除指定数据
        menuDao.deleteById(id);
    }
    //menu编辑表数据回显（通过id查询）
    @Override
    public Menu findById(Integer id) {
        Menu menu = menuDao.findById(id);
        return menu;
    }
    //menu编辑
    @Override
    public void edit(Menu menu) {
        System.out.println(menu.getName());
        System.out.println(menu.getLinkUrl());
        System.out.println(menu.getPath());
        System.out.println(menu.getPriority());
        System.out.println(menu.getIcon());
        System.out.println(menu.getDescription());
        System.out.println(menu.getParentMenuId());
        System.out.println(menu.getLevel());
        System.out.println("=====================================================================");
       menuDao.edit(menu);
       Menu m = menuDao.findById(menu.getId());
        System.out.println(m.getName());
        System.out.println(m.getLinkUrl());
        System.out.println(m.getPath());
        System.out.println(m.getPriority());
        System.out.println(m.getIcon());
        System.out.println(m.getDescription());
        System.out.println(m.getParentMenuId());
        System.out.println(m.getLevel());
    }
    //获取下拉菜单
    @Override
    public List<Menu> findParentMenu() {
        List<Menu> menus = menuDao.findParentMenu();
        return menus;
    }
    //菜单动态展示成功
    @Override
    public List<Menu> findMenuList() {
        //1.查询一级菜单
        List<Menu> parentMenu = menuDao.findParentMenu();
        //2.遍历一级菜单，在每个一级菜单中添加二级菜单 private List<Menu> children = new ArrayList<>();//子菜单集合
        //2.1遍历一级菜单
        for (Menu menu:parentMenu){
            //获取一级菜单的id
            Integer id = menu.getId();
            int  parentMenuId = id;
            //根据一级菜单的id获取二级菜单的集合
            List<Menu> menu2 = menuDao.findMenuByLevel(parentMenuId);
            //一级菜单中添加二级菜单
            menu.setChildren(menu2);
        }
        return parentMenu;
    }
}
